home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 March: Reference Library / Dev.CD Mar 97 RL.toast / mac / Technical Documentation / Macintosh Technical Notes / technotes / tn / 1076_MsgTest.hqx / MsgTest / MacMsgTest / Main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-29  |  5.7 KB  |  189 lines

  1. // •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  2. // Main.c
  3. // 
  4. // July 31, 1996
  5. // By Ben Manuto
  6. // 
  7. // © 1996 by Apple Computer, Inc., all rights reserved.
  8. //
  9. // Main entry point for our MacMsgTest tool.
  10. //
  11. // •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  12.  
  13.  
  14.  
  15.  
  16. // •••••••••••••••••
  17. // Includes
  18.  
  19. #include <Types.h>
  20. #include <LowMem.h>
  21. #include "Messaging.h"
  22. #include "MsgTest.h"
  23. #include "MsgUtil.h"
  24. #include <stdio.h>
  25.  
  26.  
  27. #define kEscKey                27                    // The decimal value for the escape key.
  28.  
  29. Boolean gSendCompCalled;                        // a global to let us know our completion routine was called.
  30. Boolean gRcvCompCalled;                            // a global to let us know our completion routine was called.
  31. Boolean gAckReceived;
  32. SInt32 gBytesReceived;                            // A global for the ack to tell us number of bytes received.
  33.  
  34.  
  35. // •••••••••••••••••
  36. // MyGetKey is a nasty little routine I copied that just reads the keyboard map to see if the 'ESC' key was pressed.
  37. // This reads and area of low-memory and will be incompatbile on future versions of the OS. This is bad! Don't do this!
  38.  
  39. char MyGetKey(void)
  40. {
  41.     if (*((char *) 0x17A) & 0x20)
  42.         return kEscKey;
  43.     
  44.     return 0;
  45. }
  46.  
  47.  
  48. // •••••••••••••••••
  49.  
  50. OSErr SendAck(SInt16 cmdBase, UInt32 bytesReceived)
  51. {
  52. MsgPBlkPtr ackMessage;
  53. OSErr error;
  54. char keyPressed = 0;
  55.  
  56.     ackMessage = NewMessage((cmdBase + kAckMsgType), bytesReceived, 0, 0);
  57.     
  58.     gSendCompCalled = false;
  59.     error = SendMessage(ackMessage);
  60.     
  61.     printf("Sending Ack to PC.......");
  62.     while ((gSendCompCalled == false) && (keyPressed != kEscKey)) {            // Hang out until our completion routine is called.
  63.         keyPressed = MyGetKey();                                            // If we hang here, offer a way out.
  64.     }
  65.     
  66.     if (keyPressed == kEscKey) {
  67.         printf("Waiting for ack to be sent was aborted!\n");
  68.         return(-1);
  69.     }
  70.     else {
  71.         printf("Ack sent to PC.\n");
  72.         return(error);
  73.     }
  74. }
  75.  
  76.  
  77.  
  78. // •••••••••••••••••
  79.  
  80. OSErr WaitForAck()
  81. {
  82. short bytes = kDefaultMsgSize;
  83. char keyPressed = 0;
  84.     
  85.     printf("Waiting for Ack from PC......");
  86.     while ((gAckReceived == false) && (keyPressed != kEscKey)) {            // Hang out until our completion routine is called.
  87.         keyPressed = MyGetKey();                                            // If we hang here, offer a way out.
  88.     }
  89.     
  90.     if (keyPressed == kEscKey) {
  91.         printf("Waiting for ack was aborted!\n");
  92.         return(-1);
  93.     }
  94.     else {
  95.         printf("Ack received from PC.\n");
  96.         if (gBytesReceived == bytes)
  97.             printf("Message was sent successfully. All %d bytes were recevied.\n", bytes);
  98.         else
  99.             printf("The Message was not received properly. %d bytes were sent and %d were received.\n", bytes, gBytesReceived);
  100.         
  101.         return(noErr);
  102.     }
  103. }
  104.  
  105.  
  106.  
  107. // •••••••••••••••••
  108.  
  109. main()
  110. {
  111. MsgPBlkPtr dataMessage, recMsg;
  112. MsgRecElemPtr theMsgRec;
  113. SInt16 i, cmdBase;
  114. OSErr error;
  115. char keyPressed = 0;
  116.  
  117.     error = InitializeMsgSystem();                                            // Initialize the messaging system.
  118.     if (error) {
  119.         printf("Could not initialize the messaging system. Err = %d\n\n", error);
  120.         return;
  121.     }
  122.     
  123.     gBytesReceived = 0;                                                        // Init this global.
  124.     
  125.     cmdBase = RegisterMessage(kMsgCommandType, kDefaultMsgTypes);            // Register a message command type and anumber of types.
  126.     if (cmdBase < 0) goto Exit;
  127.     printf("Message registered. CmdBase = %d\n", cmdBase);
  128.     
  129.     recMsg = NewMessage(cmdBase, kDefaultSelector,                             // Create a new receiver msgPBlk.
  130.                         kDefaultMsgSize, kDefaultMsgSize);    
  131.     
  132.     theMsgRec = NewMsgRecElem(cmdBase, kDefaultMsgTypes, (UInt32) recMsg);     // Create a new MsgRecElem.
  133.     printf("Message Receive Element Created.\n");
  134.     
  135.     error = InstallMsgHandler(theMsgRec);                                    // Install a message handler.
  136.     printf("Message Handler Installed. Error = %d\n", error);
  137.     
  138.     dataMessage = NewMessage(cmdBase, kDefaultSelector,                     // Create a new message PBlk
  139.                                kDefaultMsgSize, kDefaultMsgSize);    
  140.     if (dataMessage == 0L) {
  141.         printf("Unable to create message....aborting.\n");
  142.         goto Exit;
  143.     }
  144.     
  145.     gSendCompCalled = false;                                                // Mark that the send completion routine has not been called.
  146.     gRcvCompCalled = false;                                                    // Mark that the receive completion routine has not been called.
  147.     gAckReceived = false;                                                    // Mark that an acknowledge has not been received from PC.
  148.     
  149.     error = SendMessage(dataMessage);                                        // Send our message.
  150.     printf("Sending Message. Error = %d\n", error);
  151.     
  152.     printf("Waiting for Send Completion routine to be called......");
  153.     while ((gSendCompCalled == false) && (keyPressed != kEscKey)) {            // Hang out until our completion routine is called.
  154.         keyPressed = MyGetKey();                                            // If we hang here, offer a way out.
  155.     }
  156.     if (keyPressed == kEscKey) goto Exit;                                    // If we had to bail, clean up and bail.
  157.     printf("Send Completion routine called\n");
  158.     
  159.     error = WaitForAck();                                                    // Wait for Ack message from PC so we know data was sent.
  160.     if (error) goto Exit;
  161.     
  162.     printf("Waiting for Receive Completion routine to be called......");
  163.     while ((gRcvCompCalled == false) && (keyPressed != kEscKey)) {            // Hang out until our completion routine is called.
  164.         keyPressed = MyGetKey();                                            // If we hang here, offer a way out.
  165.     }
  166.     
  167.     if (keyPressed == kEscKey) goto Exit;                                    // If we had to bail, clean up and bail.
  168.     printf("Receive Completion routine called\n");
  169.     
  170.     printf("\n\nMessage Data:\n");
  171.     printf("\nParam1 = $%08lX    Param2 = $%08LX\n", 
  172.             recMsg->msgParam1, recMsg->msgParam2);
  173.     
  174.     (UInt32) (recMsg->msgBuffer) -= recMsg->msgActCount;                    // Set the ptr back to the beginning of the buffer.
  175.     
  176.     for (i = 0; i < recMsg->msgActCount; i++) {                                // Display the data we received.
  177.         printf("$%02X ", ((UInt8*)(recMsg->msgBuffer))[i]);
  178.         if (((i+1) % 16) == 0) printf("\n");
  179.     }
  180.     
  181.     error = SendAck(cmdBase, recMsg->msgActCount);                            // Send message to PC Acknowledging data received.
  182.     
  183.     Exit:
  184.         RemoveMsgHandler(theMsgRec);
  185.         printf("\nMessage Handler removed.\n");
  186.         
  187.         DeleteMsgRecElem(theMsgRec);
  188.         DeleteMessage(dataMessage);
  189. }